package com.stedu.service.impl;

import com.stedu.bean.Department;
import com.stedu.bean.Employee;
import com.stedu.mapper.DepartmentMapper;
import com.stedu.mapper.EmployeeMapper;
import com.stedu.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public Department findByDid(Integer did) {
        return departmentMapper.findByDid(did);
    }

    @Override
    public int add(Department department) {
        Department d = departmentMapper.findByDname(department.getDname());
        if(d != null) { //将要添加的名字在系统中已经存在，不允许添加
            return -1;
        }
        return departmentMapper.add(department);
    }

    @Override
    public int del(Integer did) {
        List<Employee> emps = employeeMapper.findByDid(did);
        if(emps.size() > 0) {
            return -1;
        }
        return departmentMapper.del(did);
    }

    @Override
    public int update(Department department) {

        //根据修改之后的名字查询
        Department d = departmentMapper.findByDname(department.getDname());
        if(d != null && d.getDid() != department.getDid()) {
            return -1;
        }
            return departmentMapper.update(department);
    }
}
